package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author stone
 * @date 2022/03/26 10:17
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
public class ExceptionControllerAdvice {

    @ExceptionHandler(ArithmeticException.class)
    //public ModelAndView exception() {} → 返回就是ModelAndView
    //public String exception(){}  → 使用字符串作为viewName
    //@ResponseBody//如果使用@ResponseBody返回的是Json
    public BaseRespVo exception(ArithmeticException exception) {//抛出的异常可以在形参中接收
        return BaseRespVo.fail(exception.getMessage());
    }


}
